<template>
<ul class="ul">
    <li v-for="(item, index) in lists" :key="item[id]" class="li">
        <div class="tr" :data-cate-id="item.cat_id">
            <div 
                class="td" 
                :style="'transform: translateX('+ (item.cat_level+1) * 16 +'px);'"
            >{{index+1}}</div>
            <div class="td first-tree">
                <span 
                    v-if="item.children"
                    class="open-tree" 
                    :style="'transform: translateX('+ (item.cat_level+1) * 16 +'px);'" 
                    @click="handleClickOpenCateTree"
                ></span>
                <span
                    :style="'transform: translateX('+ (item.cat_level+1) * 16 +'px);'" 
                >{{item.cat_name}}</span>
            </div>
            <div class="td">{{!Boolean(item.cat_deleted)}}</div>
            <div class="td">{{item.cat_level+1}}级</div>
            <div class="td">
                <div>
                    <button>编辑</button>
                    <button>编辑</button>
                </div>
            </div>
        </div>
        <recursion-com 
            v-if="list && list.length > 0"
            v-show="String(item.cat_id) === validataCateId"
            :list="item.children" 
            :id="id" 
        ></recursion-com>
    </li>
</ul>
</template>

<script>
export default {
    name: "recursionCom",
    props: {
        list: { /* 数据列表 */
            required: true,
            type: Array,
            default: function() {
                return [];
            }
        },
        id: { /* 属性（唯一） */
            type: String,
            default: "id"
        },
    },
    data() {
        return {
            validataCateId: ""
        }
    },
    methods: {
        handleClickOpenCateTree(e) {
            if(e.target.constructor === HTMLSpanElement) {
                const cid = e.target.parentNode.parentNode.dataset.cateId
                if(this.validataCateId === cid) {
                    this.validataCateId = ""
                    return;
                }
                this.validataCateId = cid
            }
        }
    },
    computed: {
        lists() {
            return this.list;
        }
    },
}
</script>

<style scoped>
.li {
    overflow: hidden;
    border-top: 1px solid #ee9d9d;
}
.tr {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.td {
    display: flex;
    align-items: center;
    width: 100%;
    height: 32px;
    padding: 6px 0;
    border-left: 1px solid #ee9d9d;
    box-sizing: border-box;
}
.td:nth-of-type(1) {
    min-width: 64px;
    max-width: 64px;
    width: 64px;
    border-left: none;
}
.td:nth-of-type(2) {
    min-width: 160px;
}
.td:nth-of-type(3) {
    justify-content: center;
    min-width: 80px;
}
.td:nth-of-type(4) {
    justify-content: center;
    min-width: 80px;
}
.td:nth-of-type(5) {
    justify-content: center;
    min-width: 160px;
}
.first-tree {
    display: flex;
    align-items: center;
}
.open-tree {
    position: relative;
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 5px;
    border-radius: 50%;
    background-color: #ccc;
    cursor: pointer;
}
.open-tree::before,
.open-tree::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
}
.open-tree::before {
    width: 12px;
    height: 1px;
    margin-left: -6px;
    background-color: #999;
}
.open-tree::after {
    width: 1px;
    height: 12px;
    margin-top: -6px;
    background-color: #999;
}
</style>